package com.winshare.saleAssistant.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.winshare.saleAssistant.model.SchoolHardwareMapping;
import com.winshare.saleAssistant.service.ISchoolHardwareMappingService;
import com.winshare.saleAssistant.util.LogicResponse;
import com.winshare.saleAssistant.util.PageFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/sys/schoolHardwareMapping")
public class SchoolHardwareMappingController {

    @Autowired
    private ISchoolHardwareMappingService schoolHardwareMappingService;

    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return schoolHardwareMappingService.list(null);
    }

    /**
     * 已分配列表
     */
    @RequestMapping(value = "/allocated")
    @ResponseBody
    public Object allocated(String condition) {
        Page<SchoolHardwareMapping> page = new PageFactory<SchoolHardwareMapping>().defaultPage();
        List<SchoolHardwareMapping> schools = schoolHardwareMappingService.getAllocatedList(condition, page);
        page.setRecords(schools).getRecords();
        return LogicResponse.success(page);
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Integer schoolId, String schoolHarId) {
        System.out.println(schoolId);
        System.out.println(schoolHarId);
        SchoolHardwareMapping schoolHardwareMapping = new SchoolHardwareMapping();
        for (String id : schoolHarId.split(",")) {
            schoolHardwareMapping.setSchoolId(schoolId);
            schoolHardwareMapping.setSchoolHardwareId(Integer.valueOf(id));
            schoolHardwareMappingService.save(schoolHardwareMapping);
        }
        return LogicResponse.success("success");
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer schoolHardwareMappingId) {
        schoolHardwareMappingService.removeById(schoolHardwareMappingId);
        return LogicResponse.success("success");
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(SchoolHardwareMapping schoolHardwareMapping) {
        schoolHardwareMappingService.updateById(schoolHardwareMapping);
        return LogicResponse.success("success");
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{schoolHardwareMappingId}")
    @ResponseBody
    public Object detail(@PathVariable("schoolHardwareMappingId") Integer schoolHardwareMappingId) {
        return schoolHardwareMappingService.getById(schoolHardwareMappingId);
    }
}
